package com.pms.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.pms.entity.Employee;
import com.pms.service.UserService;
import com.pms.util.Stringutil;

public class AppServlet extends HttpServlet {

	
	private static final long serialVersionUID = 1L;


	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String method=request.getParameter("m");
		
		if(Stringutil.isEmpty(method)){
			request.getRequestDispatcher("/WEB-INF/views/index.jsp").forward(request, response);
		}else if("login".equals(method)){
			/*if("post".equals(request.getMethod())){
				login(request,response);
			}else{
				response.sendError(405);
			}*/
			login(request,response);
		}
		
	}

	private void login(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		
		UserService us=new UserService();
		Employee employee=us.login(username,password);
		if(employee==null){
			response.sendRedirect("index.jspx?state=10001");
		}else{
			HttpSession session=request.getSession();
			session.setAttribute("session_employee",employee);
			
			response.sendRedirect("home.jspx");
		}
	}

}
